package com.zimmur.quartz;

import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Enumeration;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.mysql.jdbc.AbandonedConnectionCleanupThread;

@WebListener
public class ContextFinalizer implements ServletContextListener {
	private Logger log = LoggerFactory.getLogger(ContextFinalizer.class);
	
	@Override
	public void contextDestroyed(ServletContextEvent sce) {
		Enumeration<Driver> drivers = DriverManager.getDrivers();
		Driver driver =null;
		while(drivers.hasMoreElements()){
			try {
				driver=drivers.nextElement();
				DriverManager.deregisterDriver(driver);
				
			} catch (SQLException e) {
				log.error(e.getMessage());
			}
		}
		
		try {
			AbandonedConnectionCleanupThread.shutdown();
		} catch (InterruptedException e) {
			log.error(e.getMessage());
		}
	}

	@Override
	public void contextInitialized(ServletContextEvent arg0) {
		
	}


}
